package com.nosweattechnet.server;

import java.util.concurrent.ExecutionException;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nosweattechnet.datatypes.Item;


//This allows the App to create a new Item in the database
public class AddItemAsyncTask extends CommunicatorAsyncTask {
	//Constants needed for php scripts
	private BasicNameValuePair tag=new BasicNameValuePair("tag","addItem");
	private String itemNameString="item_name";
	private String itemDescripString="item_descrip";
	private String placeIDString="place_uuid";
	private String locXString="loc_x";
	private String locYString="loc_y";
	private String locZString="loc_z";
	private String userIDString="user_uuid";
	private String timeoutString="timeout";
	private Item i;
	
	//Creates a new Async task and then adds the required info
	public AddItemAsyncTask(String itemName,String itemDescrip,String placeID,Float locX,Float locY,Float locZ,String userID) {
		super();
		i=new Item(null,itemName,itemDescrip,placeID,locX,locY,locZ,userID);
		super.addNameValuePair(tag);
		super.addNameValuePair(new BasicNameValuePair(itemNameString,itemName));
		super.addNameValuePair(new BasicNameValuePair(itemDescripString,itemDescrip));
		super.addNameValuePair(new BasicNameValuePair(placeIDString,placeID));
		super.addNameValuePair(new BasicNameValuePair(locXString,locX.toString()));
		super.addNameValuePair(new BasicNameValuePair(locYString,locY.toString()));
		super.addNameValuePair(new BasicNameValuePair(locZString,locZ.toString()));
		super.addNameValuePair(new BasicNameValuePair(userIDString,userID));
	}
	
	//Creates a new Async task and then adds info, including timeout
	public AddItemAsyncTask(String itemName,String itemDescrip,String placeID,Float locX,Float locY,Float locZ,String userID,String timeout) {
		super();
		i=new Item(null,itemName,itemDescrip,placeID,locX,locY,locZ,userID);
		super.addNameValuePair(tag);
		super.addNameValuePair(new BasicNameValuePair(itemNameString,itemName));
		super.addNameValuePair(new BasicNameValuePair(itemDescripString,itemDescrip));
		super.addNameValuePair(new BasicNameValuePair(placeIDString,placeID));
		super.addNameValuePair(new BasicNameValuePair(locXString,locX.toString()));
		super.addNameValuePair(new BasicNameValuePair(locYString,locY.toString()));
		super.addNameValuePair(new BasicNameValuePair(locZString,locZ.toString()));
		super.addNameValuePair(new BasicNameValuePair(userIDString,userID));
		super.addNameValuePair(new BasicNameValuePair(timeoutString,timeout));
	}
	
	//Checks if the Item was added
	public boolean addedItem() throws InterruptedException, ExecutionException{
		try {
			JSONArray jarray = this.get();
			if(jarray==null){
				return false;
			}
			JSONObject jobj=jarray.getJSONObject(0);
			if(jobj.getInt("success")==1){
				return true;
			}
		} catch (JSONException e) {
			return false;
		}
		return false;
	}
	
	//Returns the Item that was created
	public Item getItem() throws InterruptedException, ExecutionException{
		JSONArray jarray=this.get();
		JSONObject jobj;
		try {
			jobj=jarray.getJSONObject(0);
			if(jobj.getInt("success")==1){
				String itemid=jobj.getString("item_id");
				i.assignID(itemid);
				return i;
			}
		} catch (JSONException e) {
			return null;
		}
		return null;
	}

}
